<template>
  <el-dialog :title="number.title" :visible.sync="number.open" width="25%" append-to-body :before-close="closeDelDialog">
    <el-form ref="numberForm" :rules="rules" :model="numberForm" label-width="120px">
      <el-form-item :label="number.title" prop="value">
        <el-input-number v-model="numberForm.value" controls-position="right" placeholder="请输入" :max="Infinity" style="width: 90%;"></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: "manualNumber",
  props: ["number"],
  data() {
    const checkValue = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error('输入不能为空'));
      } else if (value) {
        var reg = /^[1-9]\d*$/
        if (reg.test(value) === false) {
          callback(new Error('请输入正整数'))
        } else if(value == null) {
          callback(new Error('输入不能为空'))
        } else {
          callback()
        }
      } else {
        callback();
      }
    }
    return {
      numberForm: {
        value: undefined
      },
      rules: {
        value: [
          { trigger: 'blur', validator: checkValue }
        ]
      }
    }
  },
  methods: {
    closeDelDialog() {
      this.$refs.numberForm.resetFields();
      this.$emit("numberCancel");
      this.numberForm.value = undefined;
    },
    // 保存
    submitForm() {
      if(this.numberForm.value == null) {
        this.$modal.msgWarning("输入不能为空")
        return
      }
      if(this.numberForm.value == 0) {
        this.$modal.msgWarning("请输入正整数")
        return
      }
      // 将数字转换为字符串
      const numberString = String(this.numberForm.value);
      // 获取字符串的长度，即为数字的位数
      const digitCount = numberString.length;
      if(this.number.info.buling < digitCount) {
        this.$modal.msgWarning("超出补零限制")
        return
      }
      this.$refs["numberForm"].validate(valid => {
        if (valid) {
          this.form = {
            ruleId: this.number.info.ruleId,
            key: this.number.info.fieldName,
            value: this.numberForm.value,
            buling: this.number.info.buling
          }
          this.$emit("submitNumberForm", this.form)
          this.numberForm.value = undefined
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
